import { FrontNotifyClient } from "./fnc/frontNotifyClient";
import { guid } from "./guid";

export class FNCManager {
    static Current: FNCManager = new FNCManager();
    constructor() { }
    private fnc: FrontNotifyClient;
    get ClientId(): string {
        let r = localStorage.getItem("fncClientId");
        if (r) return r;
        else {
            let clientId = guid() + "-clerk";
            localStorage.setItem("fncClientId", clientId);
            return clientId;
        }
    };
    Connect(host: string): Promise<FrontNotifyClient> {
        if (this.IsConnect) {
            console.log("消息通知对象***", this.fnc);
            return new Promise((resolve, reject) => {
                resolve(this.fnc);
            });
        }
        else {
            if (this.fnc) {
                this.fnc.DisConnect();
                this.fnc = null;
            }
            let isHttps = location.protocol.indexOf("https") > -1;
            this.fnc = new FrontNotifyClient({
                fnsHostName: isHttps ? ("https://" + host) : ("http://" + host),
                fnsPort: isHttps ? 8896 : 8897,
                clientId: this.ClientId
            });
            console.log("消息通知对象***", this.fnc);
            return new Promise((resolve, reject) => {
                this.fnc.onConnect = () => {
                    resolve(this.fnc);
                }
            });
        }
    }
    /**
     * 发送内部消息到其他终端
     * @param  {InnerMessageType} type 消息类型
     * @param  {} content 消息内容
     */
    InnerMessageNotify(type: InnerMessageType, content) {
        if (this.IsConnect) {
            this.fnc.Notify({
                type: "innerMessage", content: {
                    headers: { name: type.Value },
                    status: 0,
                    body: content
                }
            });
        }
    }

    private app;
    Regsit_AppComponent(app) {
        this.app = app;
    }
    AppComponent_Connect() {
        this.app.FNCConnect();
    }

    get IsConnect(): boolean {
        return this.fnc && this.fnc.IsConnect;
    }
    get FNC() {
        return this.fnc;
    }
}

export class InnerMessageType {
    private name: string;
    private value: string;
    get Name() { return this.name; }
    get Value() { return this.value; }
    constructor(name: string, value: string) {
        this.name = name;
        this.value = value;
    }

    /**
     * 台桌消息
     * 
     * @static
     * 
     * @memberOf InnerMessageType
     */
    static DeskTableMsg = new InnerMessageType("deskTableMessage", "deskTable");
    /**
     * 订单消息
     * 
     * @static
     * 
     * @memberOf InnerMessageType
     */
    static OrderMsg = new InnerMessageType("OrderMessage", "orders");
    /**
     * 台桌服务消息
     * 
     * @static
     * 
     * @memberOf InnerMessageType
     */
    static TablePartyMsg = new InnerMessageType("TablePartyMessage", "tableParty");
    /**
     * 台桌状态更新消息
     */
    static TableStatusMsg = new InnerMessageType("TableStatusMessage", "update-table-status");
}